package com.microstation.controller.system;

import com.microstation.common.Assist;
import com.microstation.common.Result;
import com.microstation.dto.MenuDTO;
import com.microstation.dto.SubMenuDTO;
import com.microstation.entity.po.AdminInfo;
import com.microstation.entity.po.MenuInfo;
import com.microstation.entity.po.RoleInfo;
import com.microstation.enums.ResultEnum;
import com.microstation.service.MenuInfoService;
import com.microstation.service.RoleInfoService;
import com.microstation.util.RightsHelper;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author XuCong
 * @date 2018-02-15
 */
@RestController
@RequestMapping("/api/menu")
public class MenuController {
    private final static Logger logger = LoggerFactory.getLogger(MenuController.class);
    @Autowired
    private MenuInfoService menuInfoService;
    @Autowired
    private RoleInfoService roleInfoService;

    @RequestMapping(value = "/getMenu", method = RequestMethod.POST)
    public Result getMenu() throws Exception {
        Subject currentUser = SecurityUtils.getSubject();
        Session session = currentUser.getSession();
        try {
            AdminInfo adminInfo = (AdminInfo) session.getAttribute("adminLogin");
            if (adminInfo == null || session.getAttribute("Auths") == null) {
                session.removeAttribute("adminLogin");
                session.removeAttribute("Auths");
                session.removeAttribute("Menus");
                currentUser.logout();
                return new Result(true, ResultEnum.NOSESSION.getType(), "会话过期，请重新登录",
                        "login.html");
            }
            List<MenuDTO> menus = (List<MenuDTO>) session.getAttribute("Menus");
            if (menus == null) {
                RoleInfo roleInfo = roleInfoService.findOne(adminInfo.getRoleId());
                if (roleInfo == null) {
                    session.removeAttribute("Menus");
                    session.removeAttribute("adminLogin");
                    session.removeAttribute("Auths");
                    currentUser.logout();
                    return new Result(true, ResultEnum.NOSESSION.getType(), "账户角色不存在",
                            "login.html");
                }
                menus = new ArrayList<MenuDTO>();
                List<MenuInfo> menuInfos = null;
                List<MenuInfo> subMenuInfos = null;
                List<SubMenuDTO> subMenuDTOs = null;
                //0级菜单
                Assist assist = new Assist();
                assist.andEq("menu_flag", 0);
                menuInfos = menuInfoService.listByAssist(assist);
                assist = new Assist();
                assist.andEq("menu_flag", 1);
                for (MenuInfo menu : menuInfos) {
                    if (RightsHelper.testRights(roleInfo.getRoleRights(), "" + menu.getId())) {
                        subMenuDTOs = new ArrayList<SubMenuDTO>();
                        MenuDTO menuDTO = new MenuDTO();
                        menuDTO.setText(menu.getMenuName());
                        assist = new Assist();
                        assist.andEq("menu_flag", 1);
                        assist.andEq("parent_id", menu.getId());
                        //1级菜单
                        subMenuInfos = menuInfoService.listByAssist(assist);
                        for (MenuInfo subMenu : subMenuInfos) {
                            if (RightsHelper.testRights(roleInfo.getRoleRights(), "" + subMenu.getId())) {
                                SubMenuDTO subMenuDTO = new SubMenuDTO();
                                subMenuDTO.setHref(subMenu.getMenuUrl());
                                subMenuDTO.setId(subMenu.getMenuId());
                                subMenuDTO.setText(subMenu.getMenuName());
                                subMenuDTOs.add(subMenuDTO);
                            }
                        }
                        menuDTO.setItems(subMenuDTOs);
                        menus.add(menuDTO);
                    }
                }
                session.setAttribute("Menus", menus);
            }
            return new Result(false, ResultEnum.SUCCESS.getType(), menus);
        } catch (Exception e) {
            logger.info(ResultEnum.ERROR.getType(), e);
            return new Result(true, ResultEnum.ERROR.getType(), ResultEnum.ERROR.getInfo(), null);
        }
    }
}
